package singleton;

/**
 * @Desc 懒汉式
 * 需要时再初始化类
 * 多线程时不适用
 * @Author: yaw
 * @Date: Created in 22:25 2021/7/20
 * @Version: 1.0
 */
public class Singleton2 {
    private static Singleton2 INSTANCE;

    private Singleton2() {
    }

    public static Singleton2 getInstance() {
        if (null == INSTANCE) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton2();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
           new Thread(()->{
               System.out.println(Singleton2.getInstance().hashCode());
           }).start();
        }
    }
}
